﻿<ComClass(ClsAcadCmdStrEx.ClassId, ClsAcadCmdStrEx.InterfaceId, ClsAcadCmdStrEx.EventsId)>
Public Class ClsAcadCmdStrEx

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Const ClassId As String = "f84bc4eb-bb8d-4c21-bbc0-ea08d6e70874"
    Public Const InterfaceId As String = "c0c0a0ba-c07a-47ee-83fd-4ccbb09b5a41"
    Public Const EventsId As String = "0f9ce2b1-48d8-40ba-8aee-5789e0d5e7ec"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cir"></param>
    ''' <param name="colorIndex">0-->byblock,256-->bylayer</param>
    ''' <returns></returns>
    Public Function 创建画圆命令(cir As ClsCircle, Optional colorIndex As Short = 256) As String()
        Dim rtn As New List(Of String) From {
            "Dynmode -3"
        }
        Select Case colorIndex
            Case 0
                rtn.Add("-Color ByBlock")
            Case 256
                rtn.Add("-Color ByLayer")
            Case Else
                rtn.Add($"-Color {colorIndex}")
        End Select
        rtn.Add($"ZOOM")
        rtn.Add($"{cir.BoundingBox.LeftBtm.坐标形式}")
        rtn.Add($"{cir.BoundingBox.RightTop.坐标形式}")
        rtn.Add("Circle")
        rtn.Add($"{cir.Center.X},{cir.Center.Y},0")
        rtn.Add($"{cir.Radius}")
        Return rtn.ToArray()
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="arc"></param>
    ''' <param name="colorIndex">0-->byblock,256-->bylayer</param>
    ''' <returns></returns>
    Public Function 创建圆弧命令(arc As ClsArc, Optional colorIndex As Short = 256) As String()
        Dim rtn As New List(Of String) From {"Dynmode -3"}
        Select Case colorIndex
            Case 0
                rtn.Add("-Color ByBlock")
            Case 256
                rtn.Add("-Color ByLayer")
            Case Else
                rtn.Add($"-Color {colorIndex}")
        End Select
        rtn.Add($"arc")
        rtn.Add($"{arc.StartPoint.坐标形式}")
        rtn.Add($"{arc.MidPoint.坐标形式}")
        rtn.Add($"{arc.EndPoint.坐标形式}")
        Return rtn.ToArray()
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="line"></param>
    ''' <param name="colorIndex">0-->byblock,256-->bylayer</param>
    ''' <returns></returns>
    Public Function 创建画直线命令(line As ClsLine2d, Optional colorIndex As Short = 256) As String()
        Dim rtn As New List(Of String) From {
            "Dynmode -3"
        }
        Select Case colorIndex
            Case 0
                rtn.Add("-Color ByBlock")
            Case 256
                rtn.Add("-Color ByLayer")
            Case Else
                rtn.Add($"-Color {colorIndex}")
        End Select
        rtn.Add($"Zoom {line.BoundingBox.LeftBtm.坐标形式} {line.BoundingBox.RightTop.坐标形式}")
        rtn.Add("Line")
        rtn.Add($"{line.StartPoint.X},{line.StartPoint.Y},0")
        rtn.Add($"{line.EndPoint.X},{line.EndPoint.Y},0" & vbCr)

        Return rtn.ToArray()
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="rec"></param>
    ''' <param name="colorIndex">0-->byblock,256-->bylayer</param>
    ''' <returns></returns>
    Public Function 创建矩形命令(rec As ClsRectangle, Optional colorIndex As Short = 256) As String()
        Dim rtn As New List(Of String) From {
            "Dynmode -3"
        }
        Select Case colorIndex
            Case 0
                rtn.Add("-Color ByBlock")
            Case 256
                rtn.Add("-Color ByLayer")
            Case Else
                rtn.Add($"-Color {colorIndex}")
        End Select
        rtn.Add($"Zoom {rec.LeftBtm.坐标形式} {rec.RightTop.坐标形式}")
        rtn.Add("Pline")
        rtn.Add($"{rec.LeftBtm.坐标形式}")
        rtn.Add($"{rec.RightBtm.坐标形式}")
        rtn.Add($"{rec.RightTop.坐标形式}")
        rtn.Add($"{rec.LeftTop.坐标形式}")
        rtn.Add($"Close")
        Return rtn.ToArray()
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="tri"></param>
    ''' <param name="colorIndex">0-->byblock,256-->bylayer</param>
    ''' <returns></returns>
    Public Function 创建三角形命令(tri As ClsTriangle, Optional colorIndex As Short = 256) As String()
        Dim rtn As New List(Of String) From {
            "Dynmode -3"
        }
        Select Case colorIndex
            Case 0
                rtn.Add("-Color ByBlock")
            Case 256
                rtn.Add("-Color ByLayer")
            Case Else
                rtn.Add($"-Color {colorIndex}")
        End Select
        rtn.Add("Pline")
        rtn.Add($"{tri.PointA.坐标形式}")
        rtn.Add($"{tri.PointB.坐标形式}")
        rtn.Add($"{tri.PointC.坐标形式}")
        rtn.Add($"Close")
        Return rtn.ToArray()
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="pline"></param>
    ''' <param name="colorIndex">0-->byblock,256-->bylayer</param>
    ''' <returns></returns>
    Public Function 创建多边形命令(pline As ClsLwPolyLine, Optional colorIndex As Short = 256) As String()
        Dim rtn As New List(Of String) From {"Dynmode -3"}
        Select Case colorIndex
            Case 0
                rtn.Add("-Color ByBlock")
            Case 256
                rtn.Add("-Color ByLayer")
            Case Else
                rtn.Add($"-Color {colorIndex}")
        End Select

        Dim bbx As ClsRectangle = pline.BoundingBox
        bbx.Height *= 1.1
        bbx.Width *= 1.1
        If pline.IsOnlyLines Then
            'rtn.Add($"Zoom {bbx.LeftBtm.坐标形式} {bbx.RightTop.坐标形式}")
            For index = 0 To pline.Vertexs.Length - 1
                If index = 0 Then
                    rtn.Add($"Pline {pline.Vertexs(index).Position.坐标形式}")
                Else
                    rtn.Add($"{pline.Vertexs(index).Position.坐标形式}")
                End If
            Next
        Else
            For index = 0 To pline.Vertexs.Length - 1 Step 1
                If pline.Vertexs(index).HasBulge Then
                    Dim arc As New ClsArc
                    arc.CreatorByStartPtEndPtBulgeValue(pline.Vertexs(index).Position,
                                                        pline.Vertexs(If(index < pline.Vertexs.Length - 1, index + 1, 0)).Position,
                                                        pline.Vertexs(index).Bulge)
                    Dim arccmds = Me.创建圆弧命令(arc, colorIndex)
                    rtn.AddRange(arccmds.ToList().GetRange(2, 4))
                Else
                    rtn.Add($"line")
                    rtn.Add($"{pline.Vertexs(index).Position.坐标形式}")
                    rtn.Add($"{pline.Vertexs(If(index < pline.Vertexs.Length - 1, index + 1, 0)).Position.坐标形式}" & vbCr)
                End If
            Next
        End If

        rtn.Add($"Join select")
        rtn.Add($"window {bbx.LeftBtm.坐标形式} {bbx.RightTop.坐标形式}" & vbCr)
        Return rtn.ToArray()
    End Function

    Public Function 根据窗口句柄发送绘图命令到cad(cadHwnd As String, 命令字符串数组 As Object) As String
        If VarType(命令字符串数组) = VariantType.Array + VariantType.String Then
            Try
                Dim cmdStr As New List(Of String)
                For index = LBound(命令字符串数组) To UBound(命令字符串数组)
                    cmdStr.Add(命令字符串数组(index))
                Next
                发送命令到cad封装.SendCmd2Acad(cadHwnd, cmdStr)
                Return ""
            Catch ex As Exception
                Return ex.Message
            End Try
        Else
            Return "参数不是字符串数组"
        End If
    End Function

    ''' <summary>
    ''' 获取进程信息
    ''' </summary>
    ''' <param name="processName">进程名称.例如acad,acadlt,excel等等</param>
    ''' <param name="errstr">错误提示信息</param>
    ''' <returns></returns>
    Public Function 获取进程信息(processName As String, ByRef errstr As String) As 进程信息()
        Dim objrtns As New List(Of 进程信息)
        Dim Pros = Process.GetProcessesByName(processName).Where(Function(p) p.MainWindowTitle <> "").ToArray()
        If Pros.Length = 0 Then
            errstr = $"未开启{processName}进程"
        Else
            For Each item As Process In Pros
                Dim obj As New 进程信息
                Try

                    obj.窗口句柄 = item.MainWindowHandle
                    obj.窗口标题 = item.MainWindowTitle
                    obj.进程id = item.Id
                    obj.程序名称 = item.MainModule.FileVersionInfo.FileDescription
                    obj.程序的全路径 = item.MainModule.FileName
                    obj.程序版本 = item.MainModule.FileVersionInfo.ProductVersion
                    obj.打开的文档 = Nothing

                Catch ex As Exception
                    errstr = ex.Message & ex.HelpLink
                    obj.窗口句柄 = item.MainWindowHandle
                    obj.窗口标题 = item.MainWindowTitle
                    obj.进程id = item.Id
                    obj.程序名称 = ""
                    obj.程序的全路径 = ""
                    obj.程序版本 = ""
                    obj.打开的文档 = Nothing

                End Try

                objrtns.Add(obj)
            Next
        End If
        Return objrtns.ToArray()
    End Function

End Class


